/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package apresentacao.obra;

import java.awt.Color;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import modelo_relacional.Carro;
import modelo_relacional.Obra;
import modelo_relacional.Obra_Item;
import service.CarroService;
import service.ObraService;


/**
 *
 * @author Rui Pereira
 */
public class pesquisaObra extends javax.swing.JFrame {
    private Carro carro;
    private Obra obra;
    private Obra_Item obraItem;
    private CarroService carroService;
    private ObraService obraService;
    private List<Carro> carros;
    private List<Obra> obras;
    private List<Obra_Item> obrasItem;
    private Object[][] objectoCarro;
    private Object[][] objectoObra;
    private Object[][] objectoObraItem;
    private Carro carroUpdate;
    private Obra obraUpdate;
    private Obra_Item obraItemUpdate;
    private DefaultTableModel tableModel;
    private DefaultTableModel tableModel2;
    private DefaultTableModel tableModel3;
    private String dml;

    /**
     * Creates new form pesquisaObra
     */      
    
    public pesquisaObra() {
            initComponents();
            dml = null;
            this.carroModificarBtn.setEnabled(false);
            this.obraModificarBtn.setEnabled(false);
            this.obraItemModificarBtn.setEnabled(false);
            populaCarro();
            criaModeloTabelaCarro();
            criaModeloTabelaObra();
            criaModeloTabelaObraItem();
            setDisabledCarro();
            setDisabledObra();
            setDisabledObraItem();
            disableNovoItemBtn();
            disableNovoObraBtn();
    }

    /** 
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        carroObraTabPane = new javax.swing.JTabbedPane();
        carroPanel = new javax.swing.JPanel();
        infoCarroPanel = new javax.swing.JPanel();
        matriculaLabel = new javax.swing.JLabel();
        matriculaText = new javax.swing.JTextField();
        chassiTextF = new javax.swing.JTextField();
        anoTextF = new javax.swing.JTextField();
        kmsTextF = new javax.swing.JTextField();
        dataCarroText = new javax.swing.JTextField();
        marcaTextF = new javax.swing.JTextField();
        cavalagemTextF = new javax.swing.JTextField();
        activoTextF = new javax.swing.JTextField();
        marcaLabel = new javax.swing.JLabel();
        cavalagemLabel = new javax.swing.JLabel();
        activoLabel = new javax.swing.JLabel();
        chassiLabel = new javax.swing.JLabel();
        anoLabel = new javax.swing.JLabel();
        kmsLabel = new javax.swing.JLabel();
        dataCarroLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        carroGravarBtn = new javax.swing.JButton();
        carroModificarBtn = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();
        cancelarBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        carroTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        criaCarroLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        obraPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        matriculaLabel1 = new javax.swing.JLabel();
        matriculaObraText = new javax.swing.JTextField();
        kmsNaObraLabel = new javax.swing.JLabel();
        kmsNaObraText = new javax.swing.JTextField();
        dataObraLabel = new javax.swing.JLabel();
        dataObraText = new javax.swing.JTextField();
        descObraLabel = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        descObraText = new javax.swing.JTextArea();
        obraGravarBtn = new javax.swing.JButton();
        obraModificarBtn = new javax.swing.JButton();
        messageLabelObra = new javax.swing.JLabel();
        cancelarObraBtn = new javax.swing.JButton();
        novoObraBtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        obraTable = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        obraItemTable = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        materialLable = new javax.swing.JLabel();
        materialText = new javax.swing.JTextField();
        refMaterialLabel = new javax.swing.JLabel();
        refMaterialText = new javax.swing.JTextField();
        dataObraItemLabel = new javax.swing.JLabel();
        dataObraItemText = new javax.swing.JTextField();
        obraItemGravarBtn = new javax.swing.JButton();
        obraItemModificarBtn = new javax.swing.JButton();
        messageLabelObraItem = new javax.swing.JLabel();
        cancelarObraItemBtn = new javax.swing.JButton();
        novoItemBtn = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        criaCarroLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pesquisar/Modificar");

        infoCarroPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        matriculaLabel.setText("Matricula");

        dataCarroText.setEnabled(false);

        activoTextF.setText("S");

        marcaLabel.setText("Marca");

        cavalagemLabel.setText("Cavalagem");

        activoLabel.setText("Activo (S/N)");

        chassiLabel.setText("Chassi");

        anoLabel.setText("Ano");

        kmsLabel.setText("Kms");

        dataCarroLabel.setText("Data criação");

        jLabel1.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setText("*");

        carroGravarBtn.setText("Gravar");
        carroGravarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carroGravarBtnActionPerformed(evt);
            }
        });

        carroModificarBtn.setText("Modificar");
        carroModificarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carroModificarBtnActionPerformed(evt);
            }
        });

        cancelarBtn.setText("Cancelar");
        cancelarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout infoCarroPanelLayout = new javax.swing.GroupLayout(infoCarroPanel);
        infoCarroPanel.setLayout(infoCarroPanelLayout);
        infoCarroPanelLayout.setHorizontalGroup(
            infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoCarroPanelLayout.createSequentialGroup()
                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infoCarroPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cavalagemLabel)
                            .addComponent(marcaLabel)
                            .addComponent(activoLabel)
                            .addGroup(infoCarroPanelLayout.createSequentialGroup()
                                .addComponent(matriculaLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(matriculaText)
                            .addComponent(marcaTextF)
                            .addComponent(cavalagemTextF)
                            .addComponent(activoTextF, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(infoCarroPanelLayout.createSequentialGroup()
                                .addComponent(dataCarroLabel)
                                .addGap(18, 18, 18)
                                .addComponent(dataCarroText))
                            .addGroup(infoCarroPanelLayout.createSequentialGroup()
                                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chassiLabel)
                                    .addComponent(anoLabel)
                                    .addComponent(kmsLabel))
                                .addGap(46, 46, 46)
                                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(anoTextF, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(chassiTextF, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(kmsTextF, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(infoCarroPanelLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(infoCarroPanelLayout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(carroGravarBtn)
                .addGap(28, 28, 28)
                .addComponent(carroModificarBtn)
                .addGap(28, 28, 28)
                .addComponent(cancelarBtn)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        infoCarroPanelLayout.setVerticalGroup(
            infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoCarroPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(matriculaLabel)
                    .addComponent(matriculaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chassiLabel)
                    .addComponent(chassiTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(marcaLabel)
                    .addComponent(marcaTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anoLabel)
                    .addComponent(anoTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cavalagemLabel)
                    .addComponent(cavalagemTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kmsLabel)
                    .addComponent(kmsTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dataCarroLabel)
                        .addComponent(dataCarroText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(activoLabel)
                        .addComponent(activoTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(75, 75, 75)
                .addGroup(infoCarroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(carroGravarBtn)
                    .addComponent(carroModificarBtn)
                    .addComponent(cancelarBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 203, Short.MAX_VALUE)
                .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );

        jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        carroTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        carroTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                carroTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(carroTable);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        criaCarroLabel.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        criaCarroLabel.setIcon(new javax.swing.ImageIcon("D:\\Java\\workspace\\GestaoObrasAutoSwing\\src\\apresentacao\\images\\carro.png")); // NOI18N
        criaCarroLabel.setText("Pesquisar e modificar carro");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(criaCarroLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(criaCarroLabel))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 102));
        jLabel3.setText("Software desenvolvido por SP. Todos os direitos reservados.");

        javax.swing.GroupLayout carroPanelLayout = new javax.swing.GroupLayout(carroPanel);
        carroPanel.setLayout(carroPanelLayout);
        carroPanelLayout.setHorizontalGroup(
            carroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(carroPanelLayout.createSequentialGroup()
                .addGroup(carroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(carroPanelLayout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(infoCarroPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE))
                    .addGroup(carroPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(carroPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        carroPanelLayout.setVerticalGroup(
            carroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(carroPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(carroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(infoCarroPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE))
                .addGap(18, 45, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        carroObraTabPane.addTab("Carro", carroPanel);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        matriculaLabel1.setText("Matricula");

        kmsNaObraLabel.setText("Kms na obra");

        dataObraLabel.setText("Data criação");

        dataObraText.setEnabled(false);

        descObraLabel.setText("Descrição da obra");

        descObraText.setColumns(20);
        descObraText.setRows(5);
        jScrollPane4.setViewportView(descObraText);

        obraGravarBtn.setText("Gravar");
        obraGravarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                obraGravarBtnActionPerformed(evt);
            }
        });

        obraModificarBtn.setText("Modificar");
        obraModificarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                obraModificarBtnActionPerformed(evt);
            }
        });

        cancelarObraBtn.setText("Cancelar");
        cancelarObraBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarObraBtnActionPerformed(evt);
            }
        });

        novoObraBtn.setText("Adicionar");
        novoObraBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoObraBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane4)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(descObraLabel)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(kmsNaObraLabel)
                                            .addComponent(matriculaLabel1)
                                            .addComponent(dataObraLabel))
                                        .addGap(21, 21, 21)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(dataObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(kmsNaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(matriculaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(244, 244, 244))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(messageLabelObra, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(obraGravarBtn)
                        .addGap(34, 34, 34)
                        .addComponent(obraModificarBtn)
                        .addGap(36, 36, 36)
                        .addComponent(cancelarObraBtn)
                        .addGap(33, 33, 33)
                        .addComponent(novoObraBtn)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(matriculaLabel1)
                    .addComponent(matriculaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kmsNaObraLabel)
                    .addComponent(kmsNaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataObraLabel)
                    .addComponent(dataObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(descObraLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(obraGravarBtn)
                    .addComponent(obraModificarBtn)
                    .addComponent(cancelarObraBtn)
                    .addComponent(novoObraBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(messageLabelObra, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7))
        );

        jScrollPane2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        obraTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        obraTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                obraTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(obraTable);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        obraItemTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        obraItemTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                obraItemTableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(obraItemTable);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        materialLable.setText("Material");

        refMaterialLabel.setText("Ref. Material");

        dataObraItemLabel.setText("Data criação");

        dataObraItemText.setEnabled(false);

        obraItemGravarBtn.setText("Gravar");
        obraItemGravarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                obraItemGravarBtnActionPerformed(evt);
            }
        });

        obraItemModificarBtn.setText("Modificar");
        obraItemModificarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                obraItemModificarBtnActionPerformed(evt);
            }
        });

        cancelarObraItemBtn.setText("Cancelar");
        cancelarObraItemBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarObraItemBtnActionPerformed(evt);
            }
        });

        novoItemBtn.setText("Adicionar");
        novoItemBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoItemBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(refMaterialLabel)
                            .addComponent(materialLable)
                            .addComponent(dataObraItemLabel))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dataObraItemText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(refMaterialText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(materialText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(messageLabelObraItem, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(obraItemGravarBtn)
                        .addGap(36, 36, 36)
                        .addComponent(obraItemModificarBtn)
                        .addGap(34, 34, 34)
                        .addComponent(cancelarObraItemBtn)
                        .addGap(34, 34, 34)
                        .addComponent(novoItemBtn)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(materialLable)
                    .addComponent(materialText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(refMaterialLabel)
                    .addComponent(refMaterialText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataObraItemLabel)
                    .addComponent(dataObraItemText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(obraItemGravarBtn)
                    .addComponent(obraItemModificarBtn)
                    .addComponent(cancelarObraItemBtn)
                    .addComponent(novoItemBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(messageLabelObraItem, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        criaCarroLabel1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        criaCarroLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apresentacao/images/capacete2.png"))); // NOI18N
        criaCarroLabel1.setText("Pesquisar e modificar obra");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(criaCarroLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(criaCarroLabel1)
                .addGap(0, 6, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 102));
        jLabel2.setText("Software desenvolvido por SP. Todos os direitos reservados.");

        javax.swing.GroupLayout obraPanelLayout = new javax.swing.GroupLayout(obraPanel);
        obraPanel.setLayout(obraPanelLayout);
        obraPanelLayout.setHorizontalGroup(
            obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(obraPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(obraPanelLayout.createSequentialGroup()
                        .addGroup(obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 522, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 522, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        obraPanelLayout.setVerticalGroup(
            obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(obraPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addGroup(obraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        carroObraTabPane.addTab("Obra", obraPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(carroObraTabPane)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(carroObraTabPane))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void obraTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_obraTableMouseClicked
        getDadosTabelaObra();
        populaCamposObra();
        populaObraItem();
        criaModeloTabelaObraItem();
        enableNovoItemBtn();
        enableNovoObraBtn();
        limpaMessageLabel();
    }//GEN-LAST:event_obraTableMouseClicked

    private void carroTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_carroTableMouseClicked
        getDadosTabela();
        populaCamposCarro();
        disableNovoItemBtn();
        enableNovoObraBtn();
        limpaMessageLabel();
    }//GEN-LAST:event_carroTableMouseClicked

    private void carroModificarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carroModificarBtnActionPerformed
        setEnabledCarro ();
        this.carroGravarBtn.setEnabled(true);
        this.cancelarBtn.setEnabled(true);
    }//GEN-LAST:event_carroModificarBtnActionPerformed

    private void refreshCarroTabela() {
        populaCarro();
        criaModeloTabelaCarro();
        disableNovoItemBtn();
        disableNovoObraBtn();
    }
    
    private void carroGravarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carroGravarBtnActionPerformed
        carroService = new CarroService();
        getDadosFromCampos();
        
        try {
            carroService.criaAlteraCarro(carroUpdate, "Update");
            this.messageLabel.setForeground(Color.BLUE);
            this.messageLabel.setText("Carro modificado com sucesso. Registo interno: "+String.valueOf(carroUpdate.getCarro_id()));
            setDisabledCarro();
            this.carroGravarBtn.setEnabled(false);
            refreshCarroTabela();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro a modificar o carro.","Erro",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_carroGravarBtnActionPerformed

    private void obraModificarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_obraModificarBtnActionPerformed
        setEnabledObra ();
        this.obraGravarBtn.setEnabled(true);
        this.cancelarObraBtn.setEnabled(true);
        dml = "Update";
    }//GEN-LAST:event_obraModificarBtnActionPerformed

    private void obraGravarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_obraGravarBtnActionPerformed
        obraService = new ObraService();
        getDadosFromCamposObra();
        
        try {
            obraService.criaObra(obraUpdate, carro, dml);
            this.messageLabelObra.setForeground(Color.BLUE);
            this.messageLabelObra.setText("Obra modificada com sucesso. Registo interno: "+String.valueOf(obraUpdate.getObra_id()));
            setDisabledObra();
            this.obraGravarBtn.setEnabled(false);
            refreshDadosObra();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro a modificar a obra.","Erro",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_obraGravarBtnActionPerformed

    private void cancelarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBtnActionPerformed
        setDisabledCarro();
        this.carroGravarBtn.setEnabled(false);
        populaCamposCarro();
    }//GEN-LAST:event_cancelarBtnActionPerformed

    private void cancelarObraBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarObraBtnActionPerformed
        setDisabledObra();
        this.obraGravarBtn.setEnabled(false);
        populaCamposObra();
    }//GEN-LAST:event_cancelarObraBtnActionPerformed

    private void cancelarObraItemBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarObraItemBtnActionPerformed
        setDisabledObraItem();
        this.obraItemGravarBtn.setEnabled(false);
        populaCamposObraItem();
    }//GEN-LAST:event_cancelarObraItemBtnActionPerformed

    private void obraItemTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_obraItemTableMouseClicked
        getDadosTabelaObraItem();
        populaCamposObraItem();
        limpaMessageLabel();
    }//GEN-LAST:event_obraItemTableMouseClicked

    private void obraItemModificarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_obraItemModificarBtnActionPerformed
        setEnabledObraItem ();
        this.obraItemGravarBtn.setEnabled(true);
        this.cancelarObraItemBtn.setEnabled(true);
        dml = "Update";
    }//GEN-LAST:event_obraItemModificarBtnActionPerformed

    private void obraItemGravarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_obraItemGravarBtnActionPerformed
        obraService = new ObraService();
        getDadosFromCamposObraItem();        
        try {
            obraService.criaObraItem(obra, obraItemUpdate, dml);
            this.messageLabelObraItem.setForeground(Color.BLUE);
            this.messageLabelObraItem.setText("Material modificado com sucesso. Registo interno: "+String.valueOf(obraItemUpdate.getObra_item_id()));
            setDisabledObraItem();
            this.obraItemGravarBtn.setEnabled(false);
            refreshDadosObraItem();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro a modificar material."+e,"Erro",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_obraItemGravarBtnActionPerformed

    private void novoItemBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoItemBtnActionPerformed
        limpaDadosObraItem();
        setEnabledObraItem();
        this.obraItemGravarBtn.setEnabled(true);
        dml = "Insert";
        String timeStamp = new SimpleDateFormat("dd-MM-yyyy").format(Calendar.getInstance().getTime());
        this.dataObraItemText.setText(timeStamp);
    }//GEN-LAST:event_novoItemBtnActionPerformed

    private void novoObraBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoObraBtnActionPerformed
        limpaDadosObra();
        this.matriculaObraText.setText(carro.getMatricula());
        setEnabledObra();
        this.obraGravarBtn.setEnabled(true);
        dml = "Insert";
        String timeStamp = new SimpleDateFormat("dd-MM-yyyy").format(Calendar.getInstance().getTime());
        this.dataObraText.setText(timeStamp);
    }//GEN-LAST:event_novoObraBtnActionPerformed
/**/
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(pesquisaObra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(pesquisaObra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(pesquisaObra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(pesquisaObra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new pesquisaObra().setVisible(true);
            }
        });
    }
    
    private void limpaMessageLabel() {
        this.messageLabelObraItem.setText(null);
        this.messageLabelObra.setText(null);
        this.messageLabel.setText(null);
    }
    
    private String formataData (String data) {
        String formattedDate = null;
        Date date;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(data);
            formattedDate = new SimpleDateFormat("dd-MM-yyyy").format(date);
        } catch (ParseException ex) {
            Logger.getLogger(pesquisaObra.class.getName()).log(Level.SEVERE, null, ex);
        }
        return formattedDate;
    }
    
    private void limpaDadosCarro() {
        this.getMatriculaText().setText(null);
        this.getAnoTextF().setText(null);
        this.getCavalagemTextF().setText(null);
        this.getChassiTextF().setText(null);
        this.getKmsTextF().setText(null);
        this.getMarcaTextF().setText(null);
        this.getAnoTextF().setText(null);
        this.getDataCarroText().setText(null);
        this.carroModificarBtn.setEnabled(false);
    }
    
    private void limpaDadosObra() {
        this.getMatriculaObraText().setText(null);
        this.getDescObraText().setText(null);
        this.getKmsNaObraText().setText(null);
        this.getDataObraText().setText(null);
        this.getDataObraText().setText(null);
        this.obraModificarBtn.setEnabled(false);
    }
    
    private void limpaDadosObraItem() {
        this.getRefMaterialText().setText(null);
        this.getMaterialText().setText(null);
        this.getDataObraItemText().setText(null);
        this.obraItemModificarBtn.setEnabled(false);
    }
    
    private void setDisabledCarro () {
        this.getMatriculaText().setEnabled(false);
        this.getAnoTextF().setEnabled(false);
        this.getCavalagemTextF().setEnabled(false);
        this.getChassiTextF().setEnabled(false);
        this.getKmsTextF().setEnabled(false);
        this.getMarcaTextF().setEnabled(false);
        this.getAnoTextF().setEnabled(false);
        this.getActivoTextF().setEnabled(false);
        this.cancelarBtn.setEnabled(false);
        this.carroGravarBtn.setEnabled(false);   
    }
    
    private void setDisabledObra() {
        this.getMatriculaObraText().setEnabled(false);
        this.getKmsNaObraText().setEnabled(false);
        this.getDescObraText().setEnabled(false);
        this.cancelarObraBtn.setEnabled(false);
        this.obraGravarBtn.setEnabled(false);        
    }
    
    private void setDisabledObraItem() {
        this.getMaterialText().setEnabled(false);
        this.getRefMaterialText().setEnabled(false);
        this.cancelarObraItemBtn.setEnabled(false);
        this.obraItemGravarBtn.setEnabled(false);
    }
    
    private void enableNovoItemBtn (){
        this.novoItemBtn.setEnabled(true);
    }
    
    private void disableNovoItemBtn() {
        this.novoItemBtn.setEnabled(false);
    }
    
    private void enableNovoObraBtn (){
        this.novoObraBtn.setEnabled(true);
    }
    
    private void disableNovoObraBtn() {
        this.novoObraBtn.setEnabled(false);
    }
    
    private void setEnabledObraItem() {
        this.getMaterialText().setEnabled(true);
        this.getRefMaterialText().setEnabled(true);
        this.cancelarObraItemBtn.setEnabled(true);
        this.obraItemGravarBtn.setEnabled(true);        
        this.obraItemModificarBtn.setEnabled(false);
    }
    
    private void setEnabledCarro () {
        this.getMatriculaText().setEnabled(true);
        this.getAnoTextF().setEnabled(true);
        this.getCavalagemTextF().setEnabled(true);
        this.getChassiTextF().setEnabled(true);
        this.getKmsTextF().setEnabled(true);
        this.getMarcaTextF().setEnabled(true);
        this.getActivoTextF().setEnabled(true);
        this.cancelarBtn.setEnabled(true);
        this.carroGravarBtn.setEnabled(true);
        this.carroModificarBtn.setEnabled(false);
    }
    
    private void setEnabledObra() {
        this.getKmsNaObraText().setEnabled(true);
        this.getDescObraText().setEnabled(true);
        this.cancelarObraBtn.setEnabled(true);
        this.obraGravarBtn.setEnabled(true);
        this.obraModificarBtn.setEnabled(false);
    }
    
    private void criaModeloTabelaCarro() {
        tableModel = new DefaultTableModel(objectoCarro,new String [] {"Carro ID", "Matricula", "Chassi","Marca","Cavalagem","Ano", "Kms", "Activo", "Data Criacao"});
        carroTable  = new JTable(tableModel){ 
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false; 
        }};
        carroTable.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                carroTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(carroTable);

        carroTable.getColumnModel().getColumn(0).setMinWidth(0);
        carroTable.getColumnModel().getColumn(0).setMaxWidth(0);
        
        carroTable.getColumnModel().getColumn(2).setMinWidth(0);
        carroTable.getColumnModel().getColumn(2).setMaxWidth(0);
        
        carroTable.getColumnModel().getColumn(6).setMinWidth(0);
        carroTable.getColumnModel().getColumn(6).setMaxWidth(0);
        
        carroTable.getColumnModel().getColumn(7).setMinWidth(0);
        carroTable.getColumnModel().getColumn(7).setMaxWidth(0);
        
        carroTable.getColumnModel().getColumn(8).setMinWidth(0);
        carroTable.getColumnModel().getColumn(8).setMaxWidth(0);
    }
    
    private void populaCarro() {
        carroService = new CarroService();
        carros = new ArrayList<Carro>();
        carros = carroService.viewAllCarros();
        objectoCarro = new Object[carros.size()][9];
        int col = 0;
        int row = 0;
        
        for(Carro carro2 : carros) {
            objectoCarro[row][col] = carro2.getCarro_id();
            objectoCarro[row][++col] = carro2.getMatricula();
            objectoCarro[row][++col] = carro2.getChassi();
            objectoCarro[row][++col] = carro2.getMarca();
            objectoCarro[row][++col] = carro2.getCavalagem();
            objectoCarro[row][++col] = carro2.getAno();
            objectoCarro[row][++col] = carro2.getKms();
            objectoCarro[row][++col] = carro2.getActivo();
            objectoCarro[row][++col] = carro2.getData_criacao();
            ++row;
            col = 0;
        }
    }
    
    private void populaObra() {
        obraService = new ObraService();
        obras = new ArrayList<Obra>();
        obras = obraService.viewObras(carro.getCarro_id());
        //"Obra ID","Carro ID","Kms na Obra", "Descricao Obra", "Data da Obra"
        
        objectoObra = new Object[obras.size()][5];
        int col = 0;
        int row = 0;
        
        for(Obra obra2 : obras) {
            objectoObra[row][col] = obra2.getObra_id();
            objectoObra[row][++col] = obra2.getCarro_id();
            objectoObra[row][++col] = obra2.getKms_obra();
            objectoObra[row][++col] = obra2.getDesc();
            String data = formataData(obra2.getData_criacao());
            objectoObra[row][++col] = data;
            ++row;
            col = 0;
        }
    }
    
    private void populaObraItem() {
        obraService = new ObraService();
        obrasItem = new ArrayList<Obra_Item>();
        obrasItem = obraService.viewObraItem(obra);
        //"Obra item id","Obra ID","Carro ID", "Descricao Material", "Ref. Material", "Data do material"
        
        objectoObraItem = new Object[obrasItem.size()][6];
        int col = 0;
        int row = 0;
        
        for(Obra_Item obraItem2 : obrasItem) {
            objectoObraItem[row][col] = obraItem2.getObra_item_id();
            objectoObraItem[row][++col] = obraItem2.getObra_id();
            objectoObraItem[row][++col] = obraItem2.getCarro_id();
            objectoObraItem[row][++col] = obraItem2.getDesc_material();
            objectoObraItem[row][++col] = obraItem2.getRef_material();
            objectoObraItem[row][++col] = obraItem2.getData_criacao();
            ++row;
            col = 0;
        }
    }
    
    private void getDadosTabela() {
        int selectedRowIndex = carroTable.getSelectedRow();
        // campos da tabel: "Carro ID", "Matricula", "Chassi","Marca","Cavalagem","Ano", "Kms", "Activo", "Data Criacao"
        int carroID = (int) carroTable.getModel().getValueAt(selectedRowIndex, 0); //carro_id
        String carroMatricula = (String) carroTable.getModel().getValueAt(selectedRowIndex, 1); 
        String carroChassi = (String) carroTable.getModel().getValueAt(selectedRowIndex, 2);
        String carroMarca = (String) carroTable.getModel().getValueAt(selectedRowIndex, 3);
        String carroCavalagem = (String) carroTable.getModel().getValueAt(selectedRowIndex, 4);
        String carroAno = (String) carroTable.getModel().getValueAt(selectedRowIndex, 5);
        String carroKms = (String) carroTable.getModel().getValueAt(selectedRowIndex, 6);
        char carroActivo = (char) carroTable.getModel().getValueAt(selectedRowIndex, 7);
        String carroData = (String) carroTable.getModel().getValueAt(selectedRowIndex, 8);
        
        //setar o objecto Carro
        carro = new Carro();
        carro.setCarro_id(carroID);
        carro.setMatricula(carroMatricula);
        carro.setActivo(carroActivo);
        carro.setAno(carroAno);
        carro.setCavalagem(carroCavalagem);
        carro.setChassi(carroChassi);
        carro.setData_criacao(carroData);
        carro.setKms(carroKms);
        carro.setMarca(carroMarca);
    }
    
    private void getDadosTabelaObra() {
        int selectedRowIndex = obraTable.getSelectedRow();
        // campos da tabel: "Obra ID", "carro_id", "kms_obra","descricao", "Data Criacao"
        int obraID = (int) obraTable.getModel().getValueAt(selectedRowIndex, 0); //obra_id
        int carroID = (int) obraTable.getModel().getValueAt(selectedRowIndex, 1); //carro_id
        String obraKms = (String) obraTable.getModel().getValueAt(selectedRowIndex, 2); 
        String obraDesc = (String) obraTable.getModel().getValueAt(selectedRowIndex, 3);
        String obraData = (String) obraTable.getModel().getValueAt(selectedRowIndex, 4);
        
        //setar o objecto Obra
        obra = new Obra();
        obra.setObra_id(obraID);
        obra.setCarro_id(carroID);
        obra.setKms_obra(obraKms);
        obra.setDesc(obraDesc);
        obra.setData_criacao(obraData);
    }
    
    private void getDadosTabelaObraItem() {
        int selectedRowIndex = obraItemTable.getSelectedRow();
        // campos da tabel: "Obra_item_id", "Obra ID", "carro_id", "descricao_mate","ref_material", "Data Criacao"
        int obraItemID = (int) obraItemTable.getModel().getValueAt(selectedRowIndex, 0); //obra_item_id
        int obraID = (int) obraItemTable.getModel().getValueAt(selectedRowIndex, 1); //obra_id
        int carroID = (int) obraItemTable.getModel().getValueAt(selectedRowIndex, 2); //carro_id
        String descMate = (String) obraItemTable.getModel().getValueAt(selectedRowIndex, 3); 
        String refMate = (String) obraItemTable.getModel().getValueAt(selectedRowIndex, 4);
        String obraData = (String) obraItemTable.getModel().getValueAt(selectedRowIndex, 5);
        
        //setar o objecto Obra_Item
        obraItem = new Obra_Item();
        obraItem.setObra_item_id(obraItemID);
        obraItem.setObra_id(obraID);
        obraItem.setCarro_id(carroID);
        obraItem.setDesc_material(descMate);
        obraItem.setRef_material(refMate);
        obraItem.setData_criacao(obraData);
    }
    
    private void populaCamposObra() {
        this.matriculaObraText.setText(carro.getMatricula());
        if (obra != null) {
        this.kmsNaObraText.setText(obra.getKms_obra());
        this.descObraText.setText(obra.getDesc());
        this.dataObraText.setText(obra.getData_criacao());}
        this.obraModificarBtn.setEnabled(true);
        tableModel3.setRowCount(0); //limpa a tabela de obra_item
        limpaDadosObraItem();
    }
    
     private void populaCamposObraItem() {
        if (obraItem != null) {
        this.materialText.setText(obraItem.getDesc_material());
        this.refMaterialText.setText(obraItem.getRef_material());
        
        String data = formataData(obraItem.getData_criacao());
        this.dataObraItemText.setText(data);}
        this.obraItemModificarBtn.setEnabled(true);
    }
    
    private void getDadosFromCampos() {
        carroUpdate = new Carro();
        carroUpdate.setCarro_id(carro.getCarro_id());
        carroUpdate.setMatricula(this.matriculaText.getText());
        String activo = this.activoTextF.getText();
        carroUpdate.setActivo(activo.charAt(0));
        carroUpdate.setAno(this.anoTextF.getText());
        carroUpdate.setCavalagem(this.cavalagemTextF.getText());
        carroUpdate.setChassi(this.chassiTextF.getText());
        carroUpdate.setKms(this.kmsTextF.getText());
        carroUpdate.setMarca(this.marcaTextF.getText());
    }
    
    private void getDadosFromCamposObra () {
        obraUpdate = new Obra();
        if (obra != null) {
            obraUpdate.setObra_id(obra.getObra_id());}
        
        obraUpdate.setCarro_id(carro.getCarro_id());
        obraUpdate.setKms_obra(this.kmsNaObraText.getText());
        obraUpdate.setDesc(this.descObraText.getText());
    }
    
    private void getDadosFromCamposObraItem () {
        obraItemUpdate = new Obra_Item();
        if (obraItem != null) {
            obraItemUpdate.setObra_item_id(obraItem.getObra_item_id());}
        
        obraItemUpdate.setObra_id(obra.getObra_id());
        obraItemUpdate.setCarro_id(obra.getCarro_id());
        obraItemUpdate.setDesc_material(this.materialText.getText());
        obraItemUpdate.setRef_material(this.refMaterialText.getText());
    }
    
    private void populaCamposCarro() {
        
        this.matriculaText.setText(carro.getMatricula());
        this.marcaTextF.setText(carro.getMarca());
        this.anoTextF.setText(carro.getAno());
        this.cavalagemTextF.setText(carro.getCavalagem());
        this.chassiTextF.setText(carro.getChassi());
        this.kmsTextF.setText(carro.getKms());
        this.activoTextF.setText(String.valueOf(carro.getActivo()));
        
        String data = formataData(carro.getData_criacao());
        this.dataCarroText.setText(data); 
        this.carroModificarBtn.setEnabled(true);
        limpaDadosObra();
        this.matriculaObraText.setText(carro.getMatricula());
        limpaDadosObraItem();
        tableModel2.setRowCount(0); //limpa a tabela de obra
        refreshDadosObra();
        tableModel3.setRowCount(0); //limpa a tabela de obra_item
    }
    
    private void refreshDadosObraItem() {
        populaObraItem();
        criaModeloTabelaObraItem();
    }
    
     private void refreshDadosObra() {
        populaObra();
        criaModeloTabelaObra();
    }

    private void criaModeloTabelaObra() {
        tableModel2 = new DefaultTableModel(objectoObra,new String [] {"Obra ID","Carro ID","Kms na Obra", "Descricao Obra", "Data da Obra"});
        obraTable  = new JTable(tableModel2){ 
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false; 
        }};
        obraTable.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                obraTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(obraTable);
     
        obraTable.getColumnModel().getColumn(0).setMinWidth(0);
        obraTable.getColumnModel().getColumn(0).setMaxWidth(0);
        
        obraTable.getColumnModel().getColumn(1).setMinWidth(0);
        obraTable.getColumnModel().getColumn(1).setMaxWidth(0);
    }
    
    private void criaModeloTabelaObraItem() {
        tableModel3 = new DefaultTableModel(objectoObraItem,new String [] {"Obra Item ID","Obra ID","Carro ID","Material", "Referencia", "Data Material"});
        obraItemTable  = new JTable(tableModel3){ 
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false; 
        }};
        obraItemTable.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                obraItemTableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(obraItemTable);

        obraItemTable.getColumnModel().getColumn(0).setMinWidth(0);
        obraItemTable.getColumnModel().getColumn(0).setMaxWidth(0);
        
        obraItemTable.getColumnModel().getColumn(1).setMinWidth(0);
        obraItemTable.getColumnModel().getColumn(1).setMaxWidth(0);
        
        obraItemTable.getColumnModel().getColumn(2).setMinWidth(0);
        obraItemTable.getColumnModel().getColumn(2).setMaxWidth(0);
        
        obraItemTable.getColumnModel().getColumn(5).setMinWidth(0);
        obraItemTable.getColumnModel().getColumn(5).setMaxWidth(0);
    }
   
    // getter & setters
    public JTextField getActivoTextF() {
        return activoTextF;
    }

    public void setActivoTextF(JTextField activoTextF) {
        this.activoTextF = activoTextF;
    }

    public JTextField getAnoTextF() {
        return anoTextF;
    }

    public void setAnoTextF(JTextField anoTextF) {
        this.anoTextF = anoTextF;
    }

    public JTextField getCavalagemTextF() {
        return cavalagemTextF;
    }

    public void setCavalagemTextF(JTextField cavalagemTextF) {
        this.cavalagemTextF = cavalagemTextF;
    }

    public JTextField getChassiTextF() {
        return chassiTextF;
    }

    public void setChassiTextF(JTextField chassiTextF) {
        this.chassiTextF = chassiTextF;
    }

    public JTextField getDataCarroText() {
        return dataCarroText;
    }

    public void setDataCarroText(JTextField dataCarroText) {
        this.dataCarroText = dataCarroText;
    }

    public JTextField getKmsTextF() {
        return kmsTextF;
    }

    public void setKmsTextF(JTextField kmsTextF) {
        this.kmsTextF = kmsTextF;
    }

    public JTextField getMarcaTextF() {
        return marcaTextF;
    }

    public void setMarcaTextF(JTextField marcaTextF) {
        this.marcaTextF = marcaTextF;
    }

    public JTextField getMatriculaText() {
        return matriculaText;
    }

    public void setMatriculaText(JTextField matriculaText) {
        this.matriculaText = matriculaText;
    }

    public JTextField getDataObraItemText() {
        return dataObraItemText;
    }

    public void setDataObraItemText(JTextField dataObraItemText) {
        this.dataObraItemText = dataObraItemText;
    }

    public JTextField getDataObraText() {
        return dataObraText;
    }

    public void setDataObraText(JTextField dataObraText) {
        this.dataObraText = dataObraText;
    }

    public JTextArea getDescObraText() {
        return descObraText;
    }

    public void setDescObraText(JTextArea descObraText) {
        this.descObraText = descObraText;
    }

    public JTextField getKmsNaObraText() {
        return kmsNaObraText;
    }

    public void setKmsNaObraText(JTextField kmsNaObraText) {
        this.kmsNaObraText = kmsNaObraText;
    }

    public JTextField getMaterialText() {
        return materialText;
    }

    public void setMaterialText(JTextField materialText) {
        this.materialText = materialText;
    }

    public JTextField getMatriculaObraText() {
        return matriculaObraText;
    }

    public void setMatriculaObraText(JTextField matriculaObraText) {
        this.matriculaObraText = matriculaObraText;
    }

    public JTextField getRefMaterialText() {
        return refMaterialText;
    }

    public void setRefMaterialText(JTextField refMaterialText) {
        this.refMaterialText = refMaterialText;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel activoLabel;
    private javax.swing.JTextField activoTextF;
    private javax.swing.JLabel anoLabel;
    private javax.swing.JTextField anoTextF;
    private javax.swing.JButton cancelarBtn;
    private javax.swing.JButton cancelarObraBtn;
    private javax.swing.JButton cancelarObraItemBtn;
    private javax.swing.JButton carroGravarBtn;
    private javax.swing.JButton carroModificarBtn;
    private javax.swing.JTabbedPane carroObraTabPane;
    private javax.swing.JPanel carroPanel;
    private javax.swing.JTable carroTable;
    private javax.swing.JLabel cavalagemLabel;
    private javax.swing.JTextField cavalagemTextF;
    private javax.swing.JLabel chassiLabel;
    private javax.swing.JTextField chassiTextF;
    private javax.swing.JLabel criaCarroLabel;
    private javax.swing.JLabel criaCarroLabel1;
    private javax.swing.JLabel dataCarroLabel;
    private javax.swing.JTextField dataCarroText;
    private javax.swing.JLabel dataObraItemLabel;
    private javax.swing.JTextField dataObraItemText;
    private javax.swing.JLabel dataObraLabel;
    private javax.swing.JTextField dataObraText;
    private javax.swing.JLabel descObraLabel;
    private javax.swing.JTextArea descObraText;
    private javax.swing.JPanel infoCarroPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel kmsLabel;
    private javax.swing.JLabel kmsNaObraLabel;
    private javax.swing.JTextField kmsNaObraText;
    private javax.swing.JTextField kmsTextF;
    private javax.swing.JLabel marcaLabel;
    private javax.swing.JTextField marcaTextF;
    private javax.swing.JLabel materialLable;
    private javax.swing.JTextField materialText;
    private javax.swing.JLabel matriculaLabel;
    private javax.swing.JLabel matriculaLabel1;
    private javax.swing.JTextField matriculaObraText;
    private javax.swing.JTextField matriculaText;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JLabel messageLabelObra;
    private javax.swing.JLabel messageLabelObraItem;
    private javax.swing.JButton novoItemBtn;
    private javax.swing.JButton novoObraBtn;
    private javax.swing.JButton obraGravarBtn;
    private javax.swing.JButton obraItemGravarBtn;
    private javax.swing.JButton obraItemModificarBtn;
    private javax.swing.JTable obraItemTable;
    private javax.swing.JButton obraModificarBtn;
    private javax.swing.JPanel obraPanel;
    private javax.swing.JTable obraTable;
    private javax.swing.JLabel refMaterialLabel;
    private javax.swing.JTextField refMaterialText;
    // End of variables declaration//GEN-END:variables
}
